<%--
  Created by IntelliJ IDEA.
  User: Li Chuanwei
  Date: 2023-07-14
  Time: 10:58
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>订单详情</title>
    <script src="${pageContext.request.contextPath}/static/js/jquery.min.js"></script>
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/layui/css/layui.css" media="all">
    <link rel="stylesheet" href="${pageContext.request.contextPath}/static/css/public.css" media="all">
    <script src="${pageContext.request.contextPath}/static/layui/layui.js" charset="utf-8"></script>
    <style>
        .onClick{
            color: white;
        }
        .layui-btn-group .layui-btn-primary:hover {
            border-color: #d2d2d2;
            color: rgb(30, 159, 255);
        }
        .layui-btn-group .layui-btn-normal:hover {
            border-color: #d2d2d2;
            color: white;
        }
        .layui-table-cell {
            text-align: center;
            white-space: normal;
        }
    </style>
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <form class="layui-form layui-row" style="margin-top: 10px;margin-bottom: 10px">
            <div class="layui-inline">
                <div class="layui-input-block" style="margin-left: -9px;">
                    <label class="layui-form-label">时间选择</label>
                    <div class="layui-btn-group">
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnAll">全部</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnToday">今天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnYesterday">昨天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnLast7Days">最近7天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnLast30Days">最近30天</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnThisMonth">本月</button>
                        <button type="button" class="layui-btn btn-time layui-btn-primary" id="btnThisYear">本年</button>
                    </div>
                    <div class="layui-input-inline" style="width: 230px;">
                        <div class="layui-input-wrap">
                            <div class="layui-input-prefix">
                                <i class="layui-icon layui-icon-date"></i>
                            </div>
                            <input type="text" class="layui-input" id="ID-laydate-shortcut-range-date" placeholder="">
                        </div>
                    </div>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">订单状态</label>
                <div class="layui-col-md6">
                    <select id = "selectState">
                        <option value="">请选择</option>
                        <option value="1">已下单但未付款</option>
                        <option value="2">已付款</option>
                        <option value="3">已发货</option>
                        <option value="4">已签收</option>
                        <option value="5">已评价</option>
                    </select>
                </div>
            </div><br>
            <div style="margin-top: 15px;margin-left: -9px;">
                <div class="layui-inline" >
                    <label class="layui-form-label">用户昵称</label>
                    <div class="layui-input-inline">
                        <input type="text" name="userName" placeholder="请输入用户昵称" class="layui-input" id = "userName">
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: 50px">
                    <label class="layui-form-label">订单编号</label>
                    <div class="layui-input-inline">
                        <input type="text" name="orderId" placeholder="请输入订单编号" class="layui-input" id = "orderId">
                    </div>
                </div>
                <div class="layui-inline" style="margin-left: 50px">
                    <button id="search" class="layui-btn layui-btn-normal layui-row-xs" lay-submit lay-filter="search">查找
                    </button>
                    <button type="reset" class="layui-btn layui-btn-primary layui-row-xs">清空</button>
                </div>
            </div>
        </form>

        <table class="layui-hide" id="orderTableId" lay-filter="orderTableFilter"></table>

        <%-- 行内操作工具条--%>
        <script type="text/html" id="orderTableBar">
            {{# if(d.state === '已下单但未付款' || d.state === '已发货'){ }}
            <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            {{# } else if(d.state === '已付款'){ }}
            <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-xs" lay-event="deliver">发货</a>
            {{# } else if(d.state === '已签收'){ }}
            <a class="layui-btn layui-btn-xs" lay-event="detail">详情</a>
            <a class="layui-btn layui-btn-xs" lay-event="urge">催评价</a>
            {{# } }}
        </script>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'element','layer'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table,
            util = layui.util;
        var rate = layui.rate;
        var laydate = layui.laydate;
        var dropdown = layui.dropdown;

        // 获取所有按钮元素
        var btnAll = document.getElementById("btnAll");
        var btnToday = document.getElementById("btnToday");
        var btnYesterday = document.getElementById("btnYesterday");
        var btnLast7Days = document.getElementById("btnLast7Days");
        var btnLast30Days = document.getElementById("btnLast30Days");
        var btnThisMonth = document.getElementById("btnThisMonth");
        var btnThisYear = document.getElementById("btnThisYear");

        // 绑定点击事件处理函数
        btnAll.addEventListener("click", function() {
            var dateRange = "";
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(false,null);
        });

        btnToday.addEventListener("click", function() {
            var dateRange = getCurrentDate() + " - " + getCurrentDate();
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnYesterday.addEventListener("click", function() {
            var today = new Date();
            var yesterday = new Date(today);
            yesterday.setDate(yesterday.getDate() - 1);

            var dateRange = formatDate(yesterday) + " - " + formatDate(yesterday);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnLast7Days.addEventListener("click", function() {
            var endDate = new Date();
            var startDate = new Date(endDate);
            startDate.setDate(startDate.getDate() - 6);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnLast30Days.addEventListener("click", function() {
            var endDate = new Date();
            var startDate = new Date(endDate);
            startDate.setDate(startDate.getDate() - 29);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnThisMonth.addEventListener("click", function() {
            var today = new Date();
            var startDate = new Date(today.getFullYear(), today.getMonth(), 1);
            var endDate = new Date(today.getFullYear(), today.getMonth() + 1, 0);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        btnThisYear.addEventListener("click", function() {
            var today = new Date();
            var startDate = new Date(today.getFullYear(), 0, 1);
            var endDate = new Date(today.getFullYear(), 11, 31);

            var dateRange = formatDate(startDate) + " - " + formatDate(endDate);
            document.getElementById("ID-laydate-shortcut-range-date").value = dateRange;
            search(true,dateRange);
        });

        // 日期格式化函数
        function formatDate(date) {
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();

            return year + "-" + formatTwoDigits(month) + "-" + formatTwoDigits(day);
        }

        // 格式化为两位数字
        function formatTwoDigits(number) {
            return ("0" + number).slice(-2);
        }

        // 获取当前日期
        function getCurrentDate() {
            var today = new Date();
            return formatDate(today);
        }

        //搜索事件
        function search(quick,quickTime) {
            const inputTimeElement = document.getElementById('ID-laydate-shortcut-range-date');
            const state = document.getElementById('selectState').value;
            let timeValue = inputTimeElement.value;
            if(quick == true)
                timeValue = quickTime;
            let startTime = null;
            let endTime = null;
            if(timeValue != null && timeValue !== ''){
                const dateString = timeValue;
                const dateArray = dateString.split(" - ");
                startTime = dateArray[0];
                endTime = dateArray[1];
            }
            var userName = document.getElementById('userName').value;
            var orderId = document.getElementById('orderId').value;
            table.reload('orderTableId', {
                page: {
                    curr: 1 //重新从第 2 页开始
                }
                , where: {
                    startTime:startTime,
                    endTime:endTime,
                    userName:userName,
                    orderId:orderId,
                    state:state
                }
            }, 'data');
        }

        // 监听按钮点击事件
        $('.btn-time').on('click', function () {
            var id = this.id; // 获取按钮的id

            // 移除所有按钮的选中状态
            $('.btn-time').removeClass('layui-btn-normal onClick');
            // 设置当前按钮为选中状态
            $(this).addClass('layui-btn-normal onClick');
        });

        //日期范围
        laydate.render({
            elem: "#ID-laydate-shortcut-range-date",
            range: true,
            done:function(value){
                $('.btn-time').removeClass('layui-btn-normal onClick');
                search(false,null);
            },
            shortcuts: [
                {
                    text: "上个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        date1.setMonth(date1.getMonth() - 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                },
                {
                    text: "这个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        // date1.setMonth(date1.getMonth() - 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setMonth(date2.getMonth() + 1);
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                },
                {
                    text: "下个月",
                    value: function(){
                        var value = [];
                        var date1 = new Date();
                        date1.setMonth(date1.getMonth() + 1);
                        date1.setDate(1);
                        date1.setHours(0, 0, 0, 0);
                        value.push(date1);
                        var date2 = new Date();
                        date2.setMonth(date2.getMonth() + 2);
                        date2.setDate(1);
                        date2.setHours(0, 0, 0, 0);
                        date2 = date2.getTime() - 1;
                        value.push(new Date(date2));
                        return value;
                    }()
                }
            ]
        });

        /**
         * 表格渲染
         */
        table.render({
            elem: '#orderTableId',
            autoSort: true,
            request: {
                pageName: 'pageNum', // 页码的参数名称，默认：page
                limitName: 'pageSize' // 每页数据条数的参数名，默认：limit
            },
            url: '${pageContext.request.contextPath}/manager/order/list',
            toolbar: '#toolbar',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {type: "checkbox", width: 100,fixed: 'left'},
                {title: '序号', type: "numbers", width: 80, align: "center"},
                {field: 'id', width: 105, title: '编号', sort: true,hide:true},
                {field: 'amount', width: 120, title: '商品数量', sort: true},
                {field: 'money', width: 120, title: '订单金额', sort: true},
                {field: 'payMethodName', width: 120, title: '支付方式', sort: true},
                {field: 'userId', width: 100, title: '用户编号'},
                {field: 'userName', width: 100, title: '用户名字'},
                {field: 'addressName', width: 280, title: '收货地址'},
                {field: 'trackingName', width: 150, title: '快递公司'},
                {field: 'trackingNumber', width: 150, title: '快递单号'},
                {field: 'state', width: 130, title: '订单状态'},
                {field: 'priority', width: 120, title: '优先级'},
                {field: 'createTime', width: 180, title: '下单时间'},
                {field: 'updateTime', width: 180, title: '更新时间'},
                {fixed: 'right', title:'操作', width: 200, toolbar: '#orderTableBar'}
            ]],
            limits: [8, 15, 20, 25, 50, 100],
            limit: 8,
            page: true,
            loading: true,
            skin: 'grid',
        });

        //按钮提交事件
        form.on('submit(search)', function (data) {
            search(false,null);
            return false;
        });

        /**
         * 行内操作按钮事件
         */
        table.on('tool(orderTableFilter)', function (obj) {
            var data = obj.data;
            /**
             * 查看详情
             */
            if (obj.event === 'detail') {
                var index = layer.open({
                    title: '',
                    type: 2,
                    shade: 0.2,
                    title: false, // 不显示标题栏
                    shadeClose: true,
                    offset: '20px',
                    area:['80%', '80%'],
                    content: '${pageContext.request.contextPath}/manager/order_item/details?orderId=' + data.id,
                });
            }
            else if(obj.event === 'deliver'){
                var orderId = data.id;
                layer.open({
                    type: 1,
                    title: '输入快递信息',
                    content: '<form class="layui-form" lay-filter="expressForm" style="padding: 20px;">' +
                        '<div class="layui-form-item">' +
                        '<label class="layui-form-label">快递公司</label>' +
                        '<div class="layui-input-block">' +
                        '<input type="text" name="trackingName" required  lay-verify="required" placeholder="请输入快递公司名称" autocomplete="off" class="layui-input">' +
                        '</div></div>' +
                        '<div class="layui-form-item">' +
                        '<label class="layui-form-label">快递单号</label>' +
                        '<div class="layui-input-block">' +
                        '<input type="text" name="trackingNumber" required lay-verify="required" placeholder="请输入快递单号" autocomplete="off" class="layui-input">' +
                        '</div></div>' +
                        '<div class="layui-form-item">' +
                        '<div class="layui-input-block">' +
                        '<button class="layui-btn" lay-submit lay-filter="submitBtn">确定</button>' +
                        '</div></div></form>',
                    area: ['400px', '280px'],
                    success: function(){
                        // 表单验证
                        layui.form.render();

                        // 监听表单提交
                        layui.form.on('submit(submitBtn)', function(data){
                            // 获取表单数据
                            var formData = data.field;

                            // 发送Ajax请求并处理结果
                            $.ajax({
                                url: '${pageContext.request.contextPath}/manager/order/updateTrackingNumber?id=' + orderId,
                                type: 'GET',
                                data: formData,
                                success: function(res){
                                    // 关闭弹出层
                                    layer.closeAll();
                                    layer.msg("物流信息更新成功");
                                    table.reload('orderTableId', {}, 'data');
                                },
                                error: function(err){
                                    layer.msg("请求出现异常");
                                }
                            });

                            return false; // 阻止表单默认提交
                        });
                    }
                });
            }
        });

        /**
         * 提示栏操作
         */
        table.on('toolbar(orderTableFilter)', function (obj) {
            switch (obj.event) {
                case 'LAYTABLE_TIPS':
                    layer.alert('输入相关信息以查询订单详细信息');
                    break;
            }
            ;
        });
    });
</script>
</body>
</html>
